﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace QuangMobile
{
    public class MyCommon : System.Web.UI.Page
    {
        public string UploadFile(FileUpload ImgUploadFile)
        {
            try
            {
                if (ImgUploadFile.HasFile)
                {
                    if (ImgUploadFile.PostedFile.ContentLength < 2100000)
                    {
                        string sFolderPath = Server.MapPath(@"../userfile/Picture");
                        HttpPostedFile myFile = ImgUploadFile.PostedFile;
                        string sFileName = myFile.FileName;
                        string[] arrFileName = sFileName.Split('.');
                        arrFileName[0] = DateTime.Now.ToString("ddMMyyhhssmm");
                        sFileName = arrFileName[0] + "." + arrFileName[1];
                        myFile.SaveAs(string.Format(@"{0}\{1}", sFolderPath, sFileName));
                        return "userfile/Picture/" + sFileName;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static void Download(string sFileName, string sFilePath)
        {
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
            String Header = "Attachment; Filename=" + sFileName;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
            HttpContext.Current.Response.WriteFile(Dfile.FullName);
            HttpContext.Current.Response.End();
        }


        public static string StripHtml(string source)
        {
            string output;
            output = Regex.Replace(source, "<[^>]*>", string.Empty);
            output = Regex.Replace(output, @"^\s*$\n", string.Empty, RegexOptions.Multiline);
            return output;
        }
    }
}
